package com.syh.bishe.Util;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JwtUtils {
    private static String signKey = "syhdebiyeshejijiyuSpringBootdetanjiaoyizhendewudilewozhendehuixie"; // 生成一个足够长的密钥
    private static Long expire = 43200000L;

    public static String generateJwt(Map<String, Object> claims){
        String jwt = Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, signKey)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
        return jwt;
    }
    public static Claims parseJWT(String jwt){
        try {
            Claims claims = Jwts.parserBuilder()
                    .setSigningKey(signKey)
                    .build()
                    .parseClaimsJws(jwt)
                    .getBody();
            return claims;
        } catch (Exception e) {
            log.info("Error parsing JWT: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}
